package login

import (
	"AI/sql/disposition"
	status "AI/util"
	"database/sql"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type balance struct {
	Account      string
	Chatbalance  int
	Paintbalance int
}

func Getbalance(Data map[string]string, c *gin.Context) {
	SqlAccount, _ := c.Get("Account")
	// if Data["Account"] != SqlAccount {
	// 	c.JSON(http.StatusOK, gin.H{
	// 		"code": status.Illegal,
	// 		"msg":  "err",
	// 		"data": "非法登录,该账号已被封禁",
	// 	})
	// 	return
	// }
	// 连接数据库
	fmt.Println(SqlAccount)
	sqls := fmt.Sprintf(`select Account,Chatbalance,Paintbalance from userinfo where Account = "%s"`, SqlAccount)
	db, err := sql.Open(disposition.Usersql["sqlname"], disposition.Usersql["sqlurl"])
	info, err := db.Query(sqls)
	var Account balance
	if !info.Next() {
		c.JSON(http.StatusOK, gin.H{
			"code": status.Exit,
			"msg":  "err",
			"data": "请重新登录,谢谢",
		})
		return
	}

	err = info.Scan(&Account.Account, &Account.Chatbalance, &Account.Paintbalance)
	data := map[string]interface{}{"Account": Account.Account, "Chatbalance": Account.Chatbalance, "Paintbalance": Account.Paintbalance}
	fmt.Println(sqls, db, err)
	defer db.Close()
	c.JSON(http.StatusOK, gin.H{
		"code": status.Success,
		"msg":  "ok",
		"data": data,
	})
}
